package com.ecelin.utils;

import java.util.Comparator;

/**
 * @author ecelin@ecelin.com
 */
public class StrUtils implements Comparator<String> {
    @Override
    public int compare(String o1, String o2) {
        int length = Math.min(o1.length(), o2.length());
        for (int i = 0; i < length; i++) {
            if (o1.charAt(i) == o2.charAt(i)) {
                continue;
            }
            if (o1.charAt(i) > o2.charAt(i)) {
                return 1;
            } else {
                return -1;
            }
        }
        return 0;
    }


    public static String camelName(String field) {
        field = field.trim().toLowerCase();
        if (!field.contains("_")) {
            return field;
        }
        StringBuilder result = new StringBuilder();
        boolean nextUppercase = false;
        for (int i = 0; i < field.length(); i++) {
            char c = field.charAt(i);
            if (c == '_') {
                nextUppercase = true;
            } else {
                if (nextUppercase) {
                    result.append(Character.toUpperCase(c));
                    nextUppercase = false;
                } else {
                    result.append(Character.toLowerCase(c));
                }
            }
        }
        return result.toString();
    }

    public static String className(String tableName, String ignoredPrefix) {
        if (ignoredPrefix != null && !"".equals(ignoredPrefix.trim())) {
            String s = ",";
            if (ignoredPrefix.contains(s)) {
                String[] split = ignoredPrefix.split(s);
                for (String temp : split) {
                    if (temp == null || "".equals(temp.trim())) {
                        continue;
                    }
                    if (tableName.indexOf(temp.trim()) == 0) {
                        tableName = tableName.substring(temp.trim().length());
                    }
                }
            } else {
                if (tableName.indexOf(ignoredPrefix.trim()) == 0) {
                    tableName = tableName.substring(ignoredPrefix.trim().length());
                }
            }
        }
        String camelName = camelName(tableName);
        return camelName.substring(0, 1).toUpperCase() + camelName.substring(1);
    }

    public static boolean isNull(String string) {
        return string == null || "".equals(string.trim());
    }
}
